<?php
namespace Sms;
use Sms;
final class Auth
{
    private $username;
    private $password;
    private $_userParam;
    protected $getUrl='';
    protected $format = 'json';
    public $Charset='UTF-8';
    public $postData = array();
    //protected $Mode
    protected $API_URL = 'http://m.5c.com.cn/api/send/index.php';
    public function __construct($data)
    {
        $this->username      = $data['username'];
        $this->password      = md5($data['password']);
        $this->apikey        = $data['apikey'];
    }
    public function __set ($name, $value){
        $this->_userParam[$name] = $value;
    }

    protected function system(){
        return [
            'encode'              =>  'UTF-8',
            'username'            =>  $this->username,
            'password_md5'        =>  $this->password,
            'apikey'              =>  $this->apikey,
            //'startTime'           =>  time(),
        ];
    }
    static public function createStrParam ($paramArr)
    {
        $strParam = array();
        foreach ($paramArr as $key => $val) {
            if ($key != '' && $val != '') {
                $strParam []= $key . '=' . urlencode($val);
            }
        }
        $url = implode('&',$strParam);
        return $url;
    }
    protected function sign(){
        $data2 	        = 	self::system();
        $data	        =	array_merge($data2,$this->_userParam);
        return $data;
    }
    protected  function Url($mode){
        if(isset($this->_userParam['ApiUrl'])){
            $this->API_URL =   $this->_userParam['ApiUrl'];
            unset($this->_userParam['ApiUrl']);
        }

        $data = $this->sign();
        if($mode=='GET'){
        $strParam       = $this->createStrParam($data);
        $this->getUrl      = $this->API_URL.'?' . $strParam;
        }else{
            $this->getUrl      = $this->API_URL;
            $this->postData    =$data;
        }
    }

    public function Send ($mode = 'GET', $format = 'html'){
        $mode =  strtoupper($mode);
        $this->format = strtolower($format);
        $this->Url($mode);
        $data           =   new \Common\Extend\Api\GetData();
        
        $data->Mode     = $mode;
        $data->Url      = $this->getUrl;
        $data->format   = $format;
        $data->Charset  = $this->Charset;
        $data->postData = $this->postData;
        return $data;
    }
    /*
        success:msgid 提交成功，发送状态请见 4.1
        error:msgid 提交失败
        error:Missing username 用户名为空
        error:Missing password 密码为空
        error:Missing apikey APIKEY 为空
        error:Missing recipient 收件人手机号码为空
        error:Missing message content 短信内容为空或编码不正确
        error:Account is blocked 帐号被禁用
        error:Unrecognized encoding 编码未能识别
        error:APIKEY or password error APIKEY 或密码错误
        error:Unauthorized IP address 未授权 IP 地址
        error:Account balance is insufficient 余额不足
        error:Throughput Rate Exceeded 发送频率受限
        error:Invalid md5 password length MD5 密码长度非 32 位
        */
    public function Error($error){
        $error = explode(':',$error);
        $trl = array();
        if($error[0]=='success'){
            $status = 'success';
            $trl['status']=true;
        }else{
            $status = $error[1];
            $trl['status']=false;
        }
        $data = [
          'success'=>'发送成功',
          'fail'=>'发送失败',
          'Missing username'=>'用户名为空',
          'Missing password'=>'密码为空',
          'Missing apikey'=>'APIKEY 为空',
          'Missing recipient'=>'收件人手机号码为空',
          'Missing message content'=>'短信内容为空或编码不正确',
          'Account is blocked'=>'帐号被禁用',
          'Unrecognized encoding'=>'编码未能识别',
          'APIKEY or password error'=>'APIKEY 或密码错误',
          'Unauthorized IP address'=>'未授权 IP 地址',
          'Account balance is insufficient'=>'余额不足',
          'Throughput Rate Exceeded'=>'发送频率受限',
          'Invalid md5 password length'=>'MD5 密码长度非 32 位',
        ];
        $trl['info'] = $data[$status];
        return $trl;

    }




}
